package com.hi5.stexviewer.message.impl;

import static com.hi5.util.Validation.notEmpty;
import static com.hi5.util.Validation.state;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hi5.stexviewer.message.CallCenter;
import com.hi5.stexviewer.message.Customer;
import com.hi5.stexviewer.message.Message;

/**
 * Default implementation.
 *
 * @author mate
 *
 */
public class CallCenterImpl implements CallCenter {
    private static final Logger LOGGER = LoggerFactory.getLogger(CallCenterImpl.class);
    private Set<Customer> customers = new HashSet<Customer>();

    public void send(final Message<?> message) {
        state(notEmpty(message), "Message cannot be empty");
        state(notEmpty(message.getType()) || notEmpty(message.getRecipient()), "Either type or recipient must not be empty");
        for (Customer customer : customers) {
            if (customer != message.getSender()) {
                customer.recieve(message);
            }
        }
    }

    public void subscribe(Customer customer) {
        customers.add(customer);
        LOGGER.debug("Customer subscribed. {}", customer);
    }

    public void unsubscribe(Customer customer) {
        customers.remove(customer);
        LOGGER.debug("Customer unsubscribed. {}", customer);
    }

    public Set<Customer> getCustomers() {
        return customers;
    }

}
